package com.jim.jeecg.imvc.core.utils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import com.jim.jeecg.imvc.core.form.XmlBean;

public class Struts_config {
	
	public static Map<String,XmlBean> setStrutsXML(String xmlPath)
	{
		SAXBuilder builder=new SAXBuilder();
		Document document=new Document();
		try {
			document = builder.build(new File(xmlPath));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Element root=document.getRootElement();
		Map<String,XmlBean> rMap=new HashMap<String, XmlBean>();
		Element actionroot=root.getChild("action-mapping");
		List<Element> actions=actionroot.getChildren();
		
		for (Element element : actions) {
			
			XmlBean action=new XmlBean();
			String name=element.getAttributeValue("name");
			action.setBeanName(name);
			Element actionform=root.getChild("formbeans");
			List<Element> form=actionform.getChildren();
			for (Element ex : form) {
				if(name.equals(ex.getAttributeValue("name")))
				{
					String formClass=ex.getAttributeValue("class");
					action.setFormClass(formClass);
					break;
				}
			}
			
			String path=element.getAttributeValue("path");
			action.setPath(path);
			String type=element.getAttributeValue("type");
			action.setActionType(type);
			List<Element> forward=element.getChildren();
			Map<String,String> map=new HashMap<String, String>();
			for (Element x : forward) {
				String fname=x.getAttributeValue("name");
				String fvalue=x.getAttributeValue("value");
				map.put(fname, fvalue);
			}
			action.setActionForward(map);
			rMap.put(path, action);
		}
		
		return rMap;
	}

}
